
--注册常规合成
register_crad_rule('桃园结义',{'刘备','关羽','张飞',})
register_crad_rule('乱世奸雄',{'夏侯家族','曹氏家族','曹操',})
register_crad_rule('江东猛虎',{'孙坚','孙策','孙权',})
register_crad_rule('三国演义',{'蜀汉','曹魏','孙吴',})



--入口
local mt = ac.skill['东汉末年']
local crads = {
    --'东汉末年',
    '刘备','关羽','张飞',
    '夏侯家族','曹氏家族','曹操',
    '孙坚','孙策','孙权',
    '鬼神-吕布',
}

function mt:on_card(hero,player)
    if hero[self.name] then
        return
    end
    hero[self.name] = true
    for _,name in ipairs(crads) do
        player:set_data(name,true)
    end

    player:event '卡池-添加卡组'(function(_,_,pool)
        for _,name in ipairs(crads) do
            pool[#pool+1] = name
        end
    end)
    local count = 0
    self:gc(hero:event '单位-击杀'(function()
        count = count + 1
        self.show_num = count
        if count>=300 then
            hide_card(self)
        end
    end))
    hero:event '天赋-检测合成后'(function(trg)
        local haves = hero:get_data('拥有天赋表') or {}
        for _,name in ipairs(crads) do
            if not haves[name] then
                return
            end
        end
        trg:remove()
        hero:set_data('完成-计数',hero:get_data('完成-计数',0) + 1)
    end)
end


local mt = ac.skill['桃园结义']
local crads = {
    '马超',
    '黄忠',
    '赵云',
    '谋-诸葛',
    '魔-张飞',
    '神-刘备',
    '神-关羽',
}

function mt:on_card(hero,player)
    player:event '卡池-添加卡组'(function(_,_,pool)
        for _,name in ipairs(crads) do
            pool[#pool+1] = name
        end
    end)
    local event1 = hero:event '天赋-检测合成'(function(trg)
        local count = hero:get_data('蜀-计数',0)
        if count>=9 then
            trg:remove()
            upgrade_card(self,'蜀汉')
            for _,temp in ipairs(get_card_list(hero,'蜀')) do
                local pz = temp['品质']
                if pz~='UR' and pz~='UR+' then
                    hide_card(temp)
                end
            end
        end
    end)
    event1:execute()
    self:gc(event1)
    
    local count = 500
    self.show_num = count
    self:gc(hero:event '单位-击杀'(function()
        self.show_num  = self.show_num - 1
        if self.show_num<=0 then
            self.show_num = count
            local list = {}
            for _,temp in ipairs(get_card_list(hero,'蜀')) do
                local pz = temp['品质']
                if pz~='UR' and pz~='UR+' then
                    table.insert(list,temp)
                end
            end
            if #list>0 then
                local temp = table.random(list)
                hide_card(temp)
            end
        end
    end))
end

local mt = ac.skill['八阵图']
mt.model1 = [[cd39e59a59a4210e969a7a65adbef56e.mdl]]
function mt:on_add()
    local hero = self.owner
    self:gc(hero:add_effect(self.model1))
    self:gc(hero:add_buff '光环'{
        buff_name = '八阵图',
        area_type = 'ally',
        aura_area = 2000,
        buff_data = {skill = self,art = self.art}
    })
end

local buff = ac.buff['八阵图']
buff.cover_type = 1
buff.cover_max = 1
buff.tip = [[提升属性中]]
function buff:on_add()
    local hero = self.owner
    local skill = self.skill
    self:gc(hero:add('暴击几率',skill.data1))
    self:gc(hero:add('移动速度%',skill.data2))
    self:gc(hero:add('力量成长%',skill.data3))
    self:gc(hero:add('敏捷成长%',skill.data3))
    self:gc(hero:add('智力成长%',skill.data3))
end

local mt = ac.skill['燕人张飞在此']
mt.model1 = [[e5f11cf63b734c62aea282b887f3a212.mdl]]

function mt:on_add()
    local hero = self.owner
    local cool = 0
    self:gc(ac.loop(200,function()
        if cool<=ac.clock() and hero:get('当前生命百分比')<self.data1 then
            local damage = hero:get(self['属性2']) * self.data2/100 + hero:get(self['属性3']) * self.data3/100
            cool = ac.clock() + 60*1000
            hero:get_point():effect{
                model = self.model1,
                time = 1,
            }
            hero:aoe_damage(hero,600,damage,self)
        end
    end))
end



local mt = ac.skill['桃园结义*']
mt.temp = '攻击触发'
mt.cool = 60
mt.model1 = [[Abilities\Spells\NightElf\BattleRoar\RoarCaster.mdl]]
mt.model2 = [[d4f072a1d764ff2fdab96dc3924698c6.mdl]]
function mt:on_run(hero,target)
    local hero = self.owner
    local timer1 = ac.loop(2000,function(t)
        if not hero:is_alive() then
            t:remove()
            return
        end
        local point = hero:get_point()
        point:add_effect(self.model1):remove()
        for u in ac.selector('unit')
            : range(hero,600)
            : enemy(hero)
            : loop()
        do
            u:add_buff '张飞-破甲'{time = 2}
        end
        ac.wait(1000,function()
            local point = hero:get_point()
            local damage = hero:get('攻击') * 2
            if not hero:is_alive() then
                t:remove()
                return
            end
            point:effect{
                model = self.model2,
                size = 1,
                time = 0.2,
            }
            hero:aoe_damage(point,600,damage,self)
        end)
    end)
    ac.wait(15*1000,function()
        timer1:remove()
    end)
end

local buff = ac.buff['张飞-破甲']
function buff:on_add()
    self:gc(self.owner:add_effect([[6c10a3376824c094388be227c9b85bf3.mdl]],'overhead'))
    self:gc(self.owner:add('护甲加成',-20))
end


local mt = ac.skill['青龙偃月斩']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-攻击命中'(function(_,_,target)
        if target:get('当前生命百分比')<50 then
            local damage = hero:get('攻击') * 0.6 + hero:get('敏捷') * 1
            hero:create_damage(target,damage,self)
        end
    end))
end




local mt = ac.skill['乱世奸雄']
local crads = {
    '于禁',
    '乐进',
    '徐晃',
    '张郃',
    '武-张辽',
    '魔-典韦',
    '魔-曹操',
}

function mt:on_card(hero,player)
    player:event '卡池-添加卡组'(function(_,_,pool)
        for _,name in ipairs(crads) do
            pool[#pool+1] = name
        end
    end)
    local count = 500
    self.show_num = count
    self:gc(hero:event '单位-击杀'(function()
        self.show_num  = self.show_num - 1
        if self.show_num<=0 then
            self.show_num = count
            local list = {}
            for _,temp in ipairs(get_card_list(hero,'魏')) do
                local pz = temp['品质']
                if pz~='UR' and pz~='UR+' then
                    table.insert(list,temp)
                end
            end
            if #list>0 then
                local temp = table.random(list)
                hide_card(temp)
            end
        end
    end))
    local event1 = hero:event '天赋-检测合成'(function(trg)
        local count = hero:get_data('魏-计数',0)
        if count>=9 then
            trg:remove()
            upgrade_card(self,'曹魏')
            for _,temp in ipairs(get_card_list(hero,'魏')) do
                local pz = temp['品质']
                if pz~='UR' and pz~='UR+' then
                    hide_card(temp)
                end
            end
        end
    end)
    event1:execute()
    self:gc(event1)

end


local mt = ac.skill['威震江东']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-击杀'(function(_,_,target)
        if target:is_type('精英') then
            for u in ac.selector('unit')
                : range(target,1200)
                : ally(hero)
                : loop()
            do
                u:add_buff '威震江东'{
                    art = self.art,
                    data1 = self.data1,
                    time = self.data2,
                    max = self.data3,
                }
            end
        end
    end))
end

local buff = ac.buff['威震江东']
buff.time = 10
buff.data1 = 0
buff.tip = [[每层提升'data1'%攻速]]
buff.stack = 0
function buff:on_cover(new)
    self:set_remaining(self.time)
    if self.stack<self.max then
        self:on_add()
    end
    return false
end
function buff:on_add()
    self.stack = self.stack + 1
    self:gc(self.owner:add('攻击速度',self.data1))
end


local mt = ac.skill['双戟饮血']
mt.temp = '攻击触发'
mt.cool = 20
mt.model1 = [[a.mdl]]
function mt:on_run(hero,target)
    local point = hero:get_point()
    local damage = hero:get(self['属性1']) * self.data1/100
    local angle = point / target:get_point()
    hero:add('生命',hero:get('生命上限') * self.data2/100)
    
    local num = 2
    for a=1,num do
        local r = angle + (a-num/2 - 0.5)*30
        local m = hero:launch_wave(self.model1,r,1200,hero:get('攻击距离'),0.5)
        
        sc.loopUnit(0.03,m,150,function(_,unit)
            if unit:is_enemy(hero) then
                hero:create_damage(unit,damage,self)
            end
        end)

    end
    
end



local mt = ac.skill['奸雄']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:add_buff(self.name){
        art = self.art,
        tip = self:get_tip(),
        keep = true,
        on_add = function(buff)
            local count = 0
            buff:gc(hero:event '单位-击杀'(function(_,_,target)
                if target:is_type('精英') then
                    if count<20 then
                        count = count + 1
                        buff.stack = count
                        hero:add('攻击加成',self.data1)
                        hero:add('暴击伤害',self.data2)
                    end
                end
            end))
            buff:gc(function()
                hero:add('攻击加成',-count * self.data1)
                hero:add('暴击伤害',-count * self.data2)
            end)
        end,
    })
end

local mt = ac.skill['乱世枭雄']
mt.temp = '攻击计数'
mt.count = 12
mt.missile_art = [[12b6d777fff32c4ce20a5d56989e4e0d.mdl]]
function mt:on_run(hero,target)
    self:shot_missil(hero,target,7)
end

--弹射伤害
function mt:shot_missil(hero,dest,count)
    self.gourp = self.gourp or {}
    self.gourp[dest] = true
    local damage = hero:get('攻击') * self.data1/100 + hero:get('力量') * self.data2/100
    hero:create_damage(dest,damage,self)
    local point = dest:get_point()
    local target = ac.selector('unit'):range(dest,600):is_not(dest):enemy(hero):sorter(function(a,b)
        if self.gourp[a] and self.gourp[b]==nil then
            return false
        end
        if self.gourp[a]==nil and self.gourp[b] then
            return true
        end
		local r1 = a:get_point() * point
		local r2 = b:get_point() * point
		return r1 < r2
    end):first()
    if target then
        local start = dest:get_launch_point()
        start:launch_missile(target,self.missile_art,1200,0,function()
            count = count - 1
            if count>0 then
                self:shot_missil(hero,target,count)
            end
        end)
        
    end
end

local mt = ac.skill['江东猛虎']
local crads = {
    '鲁肃',
    '陆逊',
    '吕蒙',
    '黄盖',
    '界-徐盛',
    '界-周瑜',
    '神-孙权',
}

function mt:on_card(hero,player)
    player:event '卡池-添加卡组'(function(_,_,pool)
        for _,name in ipairs(crads) do
            pool[#pool+1] = name
        end
    end)
    local event1 = hero:event '天赋-检测合成'(function(trg)
        local count = hero:get_data('吴-计数',0)
        if count>=9 then
            trg:remove()
            upgrade_card(self,'孙吴')
            for _,temp in ipairs(get_card_list(hero,'吴')) do
                local pz = temp['品质']
                if pz~='UR' and pz~='UR+' then
                    hide_card(temp)
                end
            end
        end
    end)
    event1:execute()
    self:gc(event1)
    
    local count = 500
    self.show_num = count
    self:gc(hero:event '单位-击杀'(function()
        self.show_num  = self.show_num - 1
        if self.show_num<=0 then
            self.show_num = count
            local list = {}
            for _,temp in ipairs(get_card_list(hero,'吴')) do
                local pz = temp['品质']
                if pz~='UR' and pz~='UR+' then
                    table.insert(list,temp)
                end
            end
            if #list>0 then
                local temp = table.random(list)
                hide_card(temp)
            end
        end
    end))
end

local mt = ac.skill['疑城谋略']
mt.temp = '攻击触发'
mt.cool = 18
function mt:on_init()
    self.cool = self.data1
end

function mt:on_add()
    local hero = self.owner
    self:gc(ac.loop(self.data1*1000,function()
        if not hero:is_alive() then
            return
        end
        ac.each_player(function(player)
            local hero = player.hero
            if hero then
                hero:add_buff '护盾'{
                    art = self.art,
                    tip = [[疑城谋略护盾剩余时间]],
                    shield = hero:get('生命上限') * self.data2/100,
                    time = self.data3,
                }
            end
        end)
    end))
end


local mt = ac.skill['业火焚江']
mt.temp = '攻击触发'
mt.cool = 10
mt.model1 = [[Abilities\Weapons\FireBallMissile\FireBallMissile.mdl]]

function mt:on_run(hero,target)
    local damage = hero:get(self['属性1']) * self.data1 /100 + hero:get(self['属性2']) *self.data2/100
    local point = hero:get_point()
    local point2 = target:get_point()
    local angle = point / point2
    local lock = {}
    local a = 0
    ac.timer(200,5,function()
        a = a + 1
        local point1 = point - {angle,250*a}
        hero:launch_missile(point1,self.model1,1200,0.3,function()
            hero:aoe_damage(point1,250,damage,self,function(u)
                if lock[u.handle] then
                    return true
                end
                lock[u.handle] = true
            end)
            point1:effect{
                model = [[9d82a49a659aa7597d842c02c957cdfd.mdl]],
                speed = 0.2,
                size = 2,
                time = 5,
            }
        end)
    end)

    ac.timer(1000,5,function()
        local damage = hero:get('智力') * 1
        local group = ac.selector('unit'):line(point,angle,1000,250):enemy(hero):get()
        for _,unit in ipairs(group) do
            hero:create_damage(unit,damage,self)
        end
    end)
end


local mt = ac.skill['制衡权术']
mt.temp = '攻击触发'
mt.cool = 5
function mt:on_run(hero,target)
    ac.each_player(function(player)
        local hero = player.hero
        if hero then
            hero:add_buff '吴王号令'{time = 5,art = self.art}
        end
    end)
end

function mt:on_add()
    local hero = self.owner
    self:gc(hero:add_buff '光环'{
        buff_name = '制衡权术',
        buff_data = {skill = self,source = hero,art = self.art},
        selector = ac.selector('unit'):range(hero,2000):ally(hero),
    })
end

local buff = ac.buff['制衡权术']
buff.count = 0
buff.pulse = 1
buff.tip = [[每层提高1%攻击和1%护甲，上限15层]]
function buff:on_pulse()
    local source = self.source
    local target = self.target
    local skill = self.skill
    local rate = math.floor(source:get(skill['属性1'])/skill.data1)
    local num = math.min(rate, skill.data3) * skill.data2
    target:add('攻击加成',num - self.count)
    target:add('护甲加成',num - self.count)
    self.count = num
    self.stack = math.ceil(num)
end

function buff:on_remove()
    local target = self.target
    target:add('攻击加成', - self.count)
    target:add('护甲加成', - self.count)
end

local buff = ac.buff['吴王号令']
buff.tip = [[技能伤害+30%]]
function buff:on_add()
    self:gc(self.owner:add('技能伤害',30))
end

local mt = ac.skill['无双乱舞']
mt.temp = '攻击触发'
mt.cool = 120

function mt:on_run(hero,target)
    hero:add_buff '无双乱舞'{
        art = self.art,
        time = self.data4,
        skill = self,
    }
end


local mt = ac.buff['无双乱舞']
mt.tip = [[无双乱舞剩余时间]]
mt.model1 = [[d4dae39714a3b4f64f28bf206274525f.mdl]]
function mt:on_add()
    local hero = self.target
    local skill = self.skill
    self:gc(hero:add(skill['属性1'],skill.data1))
    self:gc(hero:add(skill['属性2'],skill.data2))
    self:gc(hero:add(skill['属性3'],skill.data3))
    self:gc(function()
        if hero:is_alive() then
            local point = hero:get_point()
            local damage = hero:get(skill['属性4']) * skill.data5/100
            point:effect{
                model = self.model1,
                angle = hero:get_facing(),
                time = 0.8,
            }
            ac.wait(400,function()
                hero:aoe_damage(hero,1500,damage,skill,function(unit)
                    local point1 = unit:get_point()
                    local angle = point/point1
                    point1:effect{
                        model = [[a951555a3eb6823199f04cff540518fb.mdl]],
                        angle = angle,
                    }:remove()
                end)
            end)
        end
    end)
end




local mt = ac.skill['鬼神之威']
mt.temp = '攻击触发'
mt.cool = 5
mt.model1 = [[eb35ec1579bd0f3c4cabf0b3630cb130.mdl]]
mt.model2 = [[Abilities\Spells\NightElf\BattleRoar\RoarCaster.mdl]]


function mt:on_run(hero,target)
    local hero = self.owner
    local angle = hero:get_point() / target:get_point()
    local unit = hero:create_unit('傻逼',hero:get_point(),angle)
    local distance = 900
    local group = {}
    local damage1 = hero:get(self['属性1']) * self.data1 / 100
    local damage2 = hero:get(self['属性1']) * self.data2 / 100
    unit:set_alpha(50)
    unit:set_model(self.model1)
    unit:add_restriction '蝗虫'
    unit:set_animation_speed(3)
    unit:set_animation(6)
    unit:loop(30,function(t)
        local dis = 1200*0.03
        local point = unit:get_point() - {angle,dis}
        distance = distance - dis
        unit:set_point(point)
        hero:aoe_damage(point,150,damage1,self,function(u)
            if group[u.handle] then
                return true
            end
            u:add_buff '击飞'{time = 0.5}
            group[u.handle] = true
        end)
        if distance<=0 then
            t:remove()
            unit:set_animation_speed(1)
            unit:set_animation('attack')
            point:add_effect(self.model2):remove()
            ac.wait(1000,function()
                hero:aoe_damage(point,600,damage2,self)
                unit:remove()
            end)
        end
    end)
end



local mt = ac.skill['仁德']
mt.cool = 60
function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-拦截死亡'(function(_,data)
        if not self:is_cooling() then
            self:cooling()
            data.life_rate = self.data1
            return true
        end
    end))
end


local mt = ac.skill['百步穿杨']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:add('攻击距离%',self.data1))
end

local mt = ac.skill['龙胆']

function mt:on_add()
    local hero = self.owner
    self:gc(ac.loop(200,function()
        if hero:get('当前生命百分比')<self.data1 then
            if self.buff==nil then
                self.buff = hero:add_buff(self.name){
                    art = self.art,
                    tip = '增加攻速',
                    keep = true,
                    on_add = function(buff)
                        buff:gc(hero:add('攻击速度',self.data2))
                    end
                }
            end
        else
            if self.buff then
                self.buff:remove()
                self.buff = nil
            end
        end
        
    end))
    
    self:gc(function()
        if self.buff then
            self.buff:remove()
            self.buff = nil
        end
    end)
end


local mt = ac.skill['铁壁']
function mt:on_add()
    local hero = self.owner
    self:gc(hero:add_buff '光环'{
        buff_name = '铁壁',
        area_type = 'ally',
        aura_area = self.data1,
        buff_data = {skill = self,art = self.art}
    })
end

local buff = ac.buff['铁壁']
buff.tip = [[护甲提升]]
function buff:on_add()
    local hero = self.owner
    local skill = self.skill
    self:gc(hero:add('护甲加成',skill.data2))
end



local mt = ac.skill['先登']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-造成伤害前'(function(_,_,target,data)
        if target.name=='腐化塔' then
            data:mul(self.data1)
        end
    end))
end


local mt = ac.skill['联盟']

function mt:on_add()
    local hero = self.owner
    self.num = 0
    self:gc(ac.loop(200,function()
        local count = (ac.player.online_count - 1) * self.data1
        hero:add('额外护甲',count - self.num)
        hero:add('额外魔抗',count - self.num)
        self.num = count
    end))
    
    self:gc(function()
        hero:add('额外护甲', - self.num)
        hero:add('额外魔抗', - self.num)
    end)
end


local mt = ac.skill['火烧连营']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-即将造成伤害'(function(_,_,target,data)
        if data.damage_type=='魔法' then
            local target = ac.selector('unit'):range(target,400):is_not(target):ally(hero):random()
            if target then
                target:damage{
                    hp_remove = true,
                    damage = data.current_damage,
                    damage_type = '魔法',
                }
            end
        end
    end))
end



local mt = ac.skill['苦肉']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-受到伤害结束'(function(_,_,_,data)
        if data.real_damage>hero:get('生命上限') * self.data1/100 then
            hero:add_buff(self.name){
                art = self.art,
                tip = '增加魔法伤害',
                keep = true,
                time = 3,
                on_add = function(buff)
                    buff:gc(hero:add(self['属性1'],self.data2))
                end
            }
        end
    end))
end
